package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysUserRoleRelateDAO;
import com.ourteam.system.dao.SysUserRoleRelate;
import com.ourteam.system.dao.SysUserRoleRelateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:38
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysUserRoleRelateDAOJdbcImpl extends AbstractSpringDAO
    implements ISysUserRoleRelateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ExpiredDate, Remarks, RoleId, Status, UserId, RoleCode, RoleName,
            RoleType, DomainId, DomainName, DomainCode
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ExpiredDate,
            _DataObjectProperty.Remarks, _DataObjectProperty.RoleId,
            _DataObjectProperty.Status, _DataObjectProperty.UserId,
            _DataObjectProperty.RoleCode, _DataObjectProperty.RoleName,
            _DataObjectProperty.RoleType, _DataObjectProperty.DomainId,
            _DataObjectProperty.DomainName, _DataObjectProperty.DomainCode
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysUserRoleRelate, SysUserRoleRelateDAOQueryBean> daoHelper;

    public AbstractSysUserRoleRelateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysUserRoleRelate, SysUserRoleRelateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysAllRoleDAO.EntityName)
                                .append(" SYS_ALL_ROLE ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysAllRoleDAO.Id,
                    com.ourteam.system.dao.ISysAllRoleDAO.EntityName + ".",
                    "SYS_ALL_ROLE."));
            relatedJoinSQLBuffer.append(" = ").append(RoleId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" SYS_DOMAIN ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "SYS_DOMAIN."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysUserRoleRelate
         * @throws Exception
         */
    public boolean checkIsUnique(SysUserRoleRelate aSysUserRoleRelate)
        throws Exception {
        if (aSysUserRoleRelate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysUserRoleRelate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysUserRoleRelate
     * @throws Exception
     */
    public void insert(SysUserRoleRelate aSysUserRoleRelate)
        throws Exception {
        super.doInsert(aSysUserRoleRelate);
    }

    /**
         * Batch Insert Datas
         * @param aSysUserRoleRelate
         * @throws Exception
         */
    public void batchInsert(SysUserRoleRelate[] aSysUserRoleRelates)
        throws Exception {
        super.doBatchInsert(aSysUserRoleRelates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysUserRoleRelateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysUserRoleRelate aSysUserRoleRelate)
        throws Exception {
        delete(aSysUserRoleRelate.getId());
    }

    /**
     * Update Data Selective
     * @param aSysUserRoleRelate
     * @throws Exception
     */
    public void update(SysUserRoleRelate aSysUserRoleRelate)
        throws Exception {
        if (aSysUserRoleRelate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysUserRoleRelate);

        SysUserRoleRelate tempObj = findById(aSysUserRoleRelate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysUserRoleRelate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysUserRoleRelates[]
     * @throws Exception
     */
    public void batchUpdate(SysUserRoleRelate[] aSysUserRoleRelates)
        throws Exception {
        super.doBatchUpdate(aSysUserRoleRelates);
    }

    /**
         * Update Data By Condictions
         * @param aSysUserRoleRelate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysUserRoleRelate aSysUserRoleRelate,
        SysUserRoleRelateDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysUserRoleRelate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysUserRoleRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysUserRoleRelate aSysUserRoleRelate)
        throws Exception {
        if (aSysUserRoleRelate.getDomainName() == null) {
            throw new Exception(
                "The SysUserRoleRelate.domainName value is not allow NULL");
        }

        if (aSysUserRoleRelate.getDomainCode() == null) {
            throw new Exception(
                "The SysUserRoleRelate.domainCode value is not allow NULL");
        }

        if (aSysUserRoleRelate.getExpiredDate() == null) {
            throw new Exception(
                "The SysUserRoleRelate.expiredDate value is not allow NULL");
        }

        if (aSysUserRoleRelate.getRemarks() == null) {
            throw new Exception(
                "The SysUserRoleRelate.remarks value is not allow NULL");
        }

        if (aSysUserRoleRelate.getRoleType() == null) {
            throw new Exception(
                "The SysUserRoleRelate.roleType value is not allow NULL");
        }

        if (aSysUserRoleRelate.getStatus() == null) {
            throw new Exception(
                "The SysUserRoleRelate.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysUserRoleRelate findById(final long id) throws Exception {
        SysUserRoleRelateDAOQueryBean queryBean = new SysUserRoleRelateDAOQueryBean();

        SysUserRoleRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysUserRoleRelate[] SysUserRoleRelates = query(queryBean);

        if (SysUserRoleRelates.length == 0) {
            return null;
        } else {
            return SysUserRoleRelates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysUserRoleRelate[]
         * @throws Exception
         */
    public SysUserRoleRelate[] query(SysUserRoleRelateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcSysUserRoleRelateRowMapper());

        return (SysUserRoleRelate[]) result.toArray(new SysUserRoleRelate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysUserRoleRelateDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysUserRoleRelateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysUserRoleRelate[] aSysUserRoleRelates,
        SysUserRoleRelateDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysUserRoleRelates, queryBean);
    }

    public void synchronize(SysUserRoleRelate[] aSysUserRoleRelates,
        SysUserRoleRelateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysUserRoleRelate> callBack)
        throws Exception {
        daoHelper.synchronize(aSysUserRoleRelates, queryBean, callBack);
    }

    public static class JdbcSysUserRoleRelateRowMapper implements RowMapper<SysUserRoleRelate> {
        public SysUserRoleRelate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysUserRoleRelate object = new SysUserRoleRelate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object expiredDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    expiredDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    expiredDate = resultSet.getObject(columnIndex++);
                }

                object.setExpiredDateNull(resultSet.getBoolean(columnIndex++));

                if (expiredDate != null) {
                    setBeanValue(object, _DataObjectProperty.ExpiredDate,
                        expiredDate);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object roleId = null;

                roleId = resultSet.getObject(columnIndex++);

                object.setRoleIdNull(resultSet.getBoolean(columnIndex++));

                if (roleId != null) {
                    setBeanValue(object, _DataObjectProperty.RoleId, roleId);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object userId = null;

                userId = resultSet.getObject(columnIndex++);

                object.setUserIdNull(resultSet.getBoolean(columnIndex++));

                if (userId != null) {
                    setBeanValue(object, _DataObjectProperty.UserId, userId);
                }

                Object roleCode = null;

                roleCode = resultSet.getObject(columnIndex++);

                object.setRoleCodeNull(resultSet.getBoolean(columnIndex++));

                if (roleCode != null) {
                    setBeanValue(object, _DataObjectProperty.RoleCode, roleCode);
                }

                Object roleName = null;

                roleName = resultSet.getObject(columnIndex++);

                object.setRoleNameNull(resultSet.getBoolean(columnIndex++));

                if (roleName != null) {
                    setBeanValue(object, _DataObjectProperty.RoleName, roleName);
                }

                Object roleType = null;

                roleType = resultSet.getObject(columnIndex++);

                object.setRoleTypeNull(resultSet.getBoolean(columnIndex++));

                if (roleType != null) {
                    setBeanValue(object, _DataObjectProperty.RoleType, roleType);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object domainName = null;

                domainName = resultSet.getObject(columnIndex++);

                object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (domainName != null) {
                    setBeanValue(object, _DataObjectProperty.DomainName,
                        domainName);
                }

                Object domainCode = null;

                domainCode = resultSet.getObject(columnIndex++);

                object.setDomainCodeNull(resultSet.getBoolean(columnIndex++));

                if (domainCode != null) {
                    setBeanValue(object, _DataObjectProperty.DomainCode,
                        domainCode);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
